package cn.doyens.controller;

import cn.doyens.bean.User;
import cn.doyens.bean.Username;
import cn.doyens.bean.Userpro;
import cn.doyens.bean.guan;
import cn.doyens.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("/li")
public class UserController {
    @Autowired
    UserService us;

    @RequestMapping("/tologin")
    public String tologin(){
        return "login";
    }

    @RequestMapping("/toblogin")
    public String toblogin(){
        return "blogin";
    }

    @RequestMapping("/login")
    public void login(guan g, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int deng = us.deng(g);
        if (deng>0){
            request.getSession().setAttribute("uu","随随便便");
            request.getSession().setAttribute("qwer",g.getName());
            request.getRequestDispatcher("/li/list").forward(request,response);
        }else {
            request.getRequestDispatcher("/li/toblogin").forward(request,response);
        }
    }
    @RequestMapping("/list")
    public String list(@RequestParam(required = true,defaultValue = "1")Integer page,HttpServletRequest request){
        PageInfo fen = us.fen(page);
        request.getSession().setAttribute("page",fen);
        return "list";
    }

    @RequestMapping("/toadd")
    public String toadd(){
        return "add";
    }

    @RequestMapping("/add")
    public void add(Userpro user, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        us.insert(user);
        request.getRequestDispatcher("/li/list").forward(request,response);
    }

    @RequestMapping("/toupdate")
    public String toupdate(Integer id,HttpServletRequest request,Model model){
        request.getSession().setAttribute("id",id);
        User byid = us.findByid(id);
        String projectid = byid.getProjectid();
        if (projectid.equals("1")){
            model.addAttribute("xiaozu","第一组");
        }else if(projectid.equals("2")){
            model.addAttribute("xiaozu","第二组");
        }else {
            model.addAttribute("xiaozu","第三组");
        }
        model.addAttribute("byid",byid);
        return "Update";
    }

    @RequestMapping("/update")
    public void update(Userpro user,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        Integer id = (Integer) request.getSession().getAttribute("id");
        user.setId(id);
        us.update(user);
        request.getRequestDispatcher("/li/list").forward(request,response);
    }


    @RequestMapping("/delete")
    public void deletebyid(Integer id,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        us.deletebyid(id);
        PageInfo page = (PageInfo) request.getSession().getAttribute("page");
        int pageNum = page.getPageNum();
        request.getRequestDispatcher("/li/list?page="+pageNum).forward(request,response);

    }
    @RequestMapping("/fanhui")
    public void fanhui(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        PageInfo page = (PageInfo) request.getSession().getAttribute("page");
        int pageNum = page.getPageNum();
        request.getRequestDispatcher("/li/list?page="+pageNum).forward(request,response);

    }
    @RequestMapping("/tomh")
    public void tomh(Username user,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        request.getSession().setAttribute("user",user);
        System.out.println(user);
        request.getRequestDispatcher("/li/toblist").forward(request,response);
    }

    @RequestMapping("/toblist")
    public String toblist(@RequestParam(required = true,defaultValue = "1")Integer page,HttpServletRequest request,HttpServletResponse response){
        Username user = (Username) request.getSession().getAttribute("user");
        PageInfo pageInfo = us.fen2(page, user);
        request.getSession().setAttribute("page2",pageInfo);
 return "blist";
    }
}
